package com.yfg.wap.flight.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

@SuppressWarnings({"rawtypes","unchecked"})
public class GlobalData extends HttpServlet{
	private static final long serialVersionUID = 4350397684939746417L;
	public static TreeMap cityMap;
	public static TreeMap flightMap;
	
	@Override
	public void init() throws ServletException {
		if(!Tools.isNotNullOrEmpty(GlobalData.cityMap)){
			String fileurl = GlobalData.class.getResource("/properties/citycode_zh.properties").getPath();
			GlobalData.cityMap=initLoadProperties(fileurl, GlobalData.cityMap);
//			System.out.println(GlobalData.cityMap);
		}
		if(!Tools.isNotNullOrEmpty(GlobalData.flightMap)){
			String fileurl = GlobalData.class.getResource("/properties/flightname_zh.properties").getPath();
			GlobalData.flightMap=initLoadProperties(fileurl, GlobalData.flightMap);
//			System.out.println(GlobalData.flightMap);
		}
	}


	private static TreeMap initLoadProperties(String fileurl,TreeMap datamap){
		Properties properties = new Properties();
		fileurl=fileurl.replaceAll("%20", " ");
		try {
			properties.load(new FileInputStream(fileurl));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return packPropertiesCode(datamap, properties);
	}
	
	
	private static TreeMap packPropertiesCode(TreeMap datamap, Properties	prop){
		datamap = new TreeMap();
		Enumeration enumers = prop.propertyNames();
		while (enumers.hasMoreElements()) {
			String key = (String) enumers.nextElement();
			datamap.put(key, prop.get(key));
		}
		return datamap;
	}
	
}
